/**
 * Object defining an acquired article
 */

//package comparable.ex01;


public class article implements Comparable{
	private String title;
	private String time;
	private String link;
	private String description;
	private String source;
	private String text;
	private String topic;
	
	//Localness ranking
	public double localRank;
	public double location;
	
	//1 for news ; 2 for business ; 3 for entertainment ; 4 for sports
	public int category;
	
	//Flag indicating whether text is parsed properly
	private int valid;
	
	
	/**
	 * Basic constructor for articles
	 */
	public article(){
		title="";
		time="";
		link="";
		description="";
		source = "";
		text = "";
		valid = 0;
		topic="";
		category = 0;
		localRank=0;
		location = 0;
	}
	
	
	/**
	 * Constructor with already defined link
	 */
	public article(String l){
		title="";
		time="";
		link=l;
		description="";
		source = "";
		topic="";
		text = "";
		valid = 0;
		category = 0;
		localRank=0;
		location = 0;
	}
	
	/**
	 * Constructor for training article
	 */
	public article(String tit, String l, String s, String te, String t)
	{
		title=tit;
		time="";
		link=l;
		description="";
		source = s;
		text = removeInv(te);
		valid = 0;
		topic=t;
		category = 0;
		localRank=0;
		location = 0;
	}
	
	public int compareTo(Object anotherarticle) throws ClassCastException {
	    if (!(anotherarticle instanceof article))
	      throw new ClassCastException("A article object expected.");
	    double anotherarticleRank = ((article) anotherarticle).localRank;  
	    if((this.localRank - anotherarticleRank)<0) return 1;
	    return -1;
	  }

	
	/**
	 * Check that all details for article have been accurately acquired
	 * @return
	 */
	public int checkArt()
	{
		if(title.length() < 1) return 0;
		if(time.length() < 1) return 0;
		if(link.length() < 1) return 0;
		if(source.length() < 1) return 0;
		if(description.length() < 1) return 0;
		
		//If source is Financial Times then not valid
		if(source.equals("Financial Times, UK")) return 0;
		
		//Dont include news sources that require subscriptions
		if(source.contains("(subscription)")) return 0;
		
		//Dont include Wall Street Journal 
		if(source.equals("Wall Street Journal")) return 0;
		
		//Dont include press releases
		if(source.contains("(press release)")) return 0;
		
		//Crain's is now a subsrciption site therefore unreadable
		if(source.contains("Crain's Manchester Business")) return 0;
		
		
		return 1;
	}
	
	/**
	 * Title Retrieval
	 */
	public String getTitle()
	{
		return title;
	}
	
	
	/**
	 * Title Setting
	 */
	public void setTitle(String s)
	{
		title = s;
	}
	
	/**
	 * Time Retrieval
	 */
	public String getTime()
	{
		return time;
	}
	
	/**
	 * Time Setting
	 */
	public void setTime(String s)
	{
		time = s;
	}
	
	/**
	 * Link Retrieval
	 */
	public String getLink()
	{
		return link;
	}
	
	/**
	 * Link Setting
	 */
	public void setLink(String s)
	{
		link = s;
	}
	
	/**
	 * Description Retrieval
	 */
	public String getDescription()
	{
		return description;
	}
	
	/**
	 * Description Setting
	 */
	public void setDescription(String s)
	{
		int index;

		if((index = s.indexOf("»")) > -1) s = s.substring(index + 3);
		description = removeInv(s);
	}
	
	/**
	 * Source Retrieval
	 */
	public String getSource()
	{
		return source;
	}
	
	/**
	 * Source Setting
	 */
	public void setSource(String s)
	{
		source = removeInv(s);
	}
	
	/**
	 * text Retrieval
	 */
	public String getText()
	{
		return text;
	}
	
	/**
	 * text Setting
	 */
	public void setText(String s)
	{
		text = removeInv(s);
	}
	
	/**
	 * Valid Retrieval
	 */
	public int getValid()
	{
		return valid;
	}
	
	/**
	 * Valid Setting
	 */
	public void setValid(int s)
	{
		valid = s;
	}
	
	/**
	 * Topic Retrieval
	 */
	public String getTopic()
	{
		return topic;
	}
	
	/**
	 * Topic Setting
	 */
	public void setTopic(String s)
	{
		topic = s;
	}
	
	/**
	 * Removes invlaid char
	 */
	private String removeInv(String s)
	{
		int t;
		char[] data = s.toCharArray();
		
		for(int i=0;i<data.length;i++)
		{
			t=data[i];
			if((t<32) || (t>125) || (t==38)) data[i] = 32;
		}
		
		return new String(data);
	}
}
